<?php
namespace App\Repositories\Presenter;

class MenuPresenter
{
	/**
	 * 0级菜单
	 */
	public function getMenu($menus,$selected = -1)
	{
		if ($menus) {
			$option = '<option value="0">顶级菜单</option>';
			foreach ($menus as $v) {
				$h="";
				for($i=1;$i<$v->level;$i++){
					$h .= "&nbsp;—";
				}
				$option .= '<option value="'.$v->id.'" ';
				if($v->id == $selected){
					$option .= "selected ";
				}
				$option .= '>'.$h.$v->name.'</option>';
			}
			return $option;
		}
		return '<option value="0">顶级菜单</option>';
	}

	/**
	 * 菜单列表
	 */
	public function getMenuList($menus)
	{
		if ($menus) {
			$item = $this->getNestableItem($menus);
			return $item;
		}
		return '暂无菜单';
	}

	/**
	 * 递归生成nestable
	 */
	protected function getNestableItem($menus,$level = 1)
	{
		$thisItem = '<ol class="dd-list">';
		foreach($menus as $v){
			$tempItem = '<li class="dd-item dd3-item" data-id="'.$v['id'].'"><div class="dd-handle dd3-handle">Drag</div>';
			$tempItem .= '<div class="dd3-content">'.$v['name'].'<div class="pull-right action-buttons">';
			if($v['child']){
			}else{
				$tempItem .= '<a href="javascript:;"  data-id="'.$v['id'].'" class="btn-xs tooltips destoryMenu" data-placement="top"><i class="fa fa-trash"></i><form method="POST" name="delete_item_'.$v['id'].'" action="'.route('menu.destroy',['id'=>$v['id']]).'"  style="display:none"><input type="hidden" name="_method" value="delete"><input type="hidden" name="_token" value="'.csrf_token().'"></form></a>';
			}



			$tempItem .= '<a href="javascript:;" class="btn-xs tooltips editMenu" data-href="'.route('menu.edit',['id'=>$v['id']]).'" data-placement="top" ><i class="fa fa-pencil"></i></a>';
			$tempItem .= '</div></div>';
			if($v['child']){
				$childItem = self::getNestableItem($v['child'],$level+1);
				$tempItem.=$childItem;
			}
			$tempItem .= '</li>';

			$thisItem .=$tempItem;
		}
		$thisItem .='</ol>';
		return $thisItem;

	}

	public function getSideBar($menus){
		if ($menus) {
			$item = $this->getTopSideBar($menus);
			return $item;
		}
		return '';
	}

	/**
	 * 获取菜单顶层
	 */
	protected function getTopSideBar($menus){
		$thisItem = "";
		foreach($menus as $v){
			if(!sp_check_auth($v['permission'])){
				continue;
			}
			$thisItem .="<li><a ";
			if($v['is_url']){
				$thisItem.= "href = '".route($v['permission'])."'";
			}
			$thisItem .= "><i class='".$v['icon']."'></i>".$v['name'];

			if(empty($v['child'])){
				$thisItem.="</a>";
			}else{
				$childArray = $this->getChildSideBar($v['child']);
				if($childArray['count']){
					$thisItem.="<span class='fa fa-chevron-down'></span></a>";
				}else{
					$thisItem.="</a>";
				}
				$thisItem .= $childArray['htmlItem'];
			}
			$thisItem.="</li>";
		}
		return $thisItem;
	}
	/**
	 * 获取儿子菜单
	 */
	protected function getChildSideBar($menus){
		$childCount = 0;
		$thisItem ="";
		foreach($menus as $v){
			if(!sp_check_auth($v['permission'])){
				continue;
			}
			if($v['is_show']){
				$childCount ++;
				$thisItem .="<li><a ";
				if($v['is_url']){
					$thisItem .= "href = '".route($v['permission'])."'";
				}
				$thisItem .= ">".$v['name'];

				if(empty($v['child'])){
					$thisItem.="</a>";
				}else{
					$childArray = $this->getChildSideBar($v['child']);
					if($childArray['count']){
						$thisItem.="<span class='fa fa-chevron-down'></span></a>";
					}else{
						$thisItem.="</a>";
					}
					$thisItem .= $childArray['htmlItem'];
				}
				$thisItem.="</li>";
			}
		}
		if($childCount){
			$thisItem = '<ul class="nav child_menu">'.$thisItem."</ul>";
		}
		return [
				"count"=>$childCount,
				"htmlItem"=>$thisItem
			];
	}
}
